package right_content

import (
	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/canvas"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/layout"
	"fyne.io/fyne/v2/theme"
	"fyne.io/fyne/v2/widget"
	"gitee.com/huanminabc/go-utils/common"
	log "github.com/sirupsen/logrus"
	"go-CodeToolCmd/java/apifox"
	"go-CodeToolUi/core"
	"go-CodeToolUi/utils"
	"runtime/debug"
	"strings"
)

type ApiFox struct {
	root         fyne.CanvasObject
	files        []*widget.Entry
	params       []*widget.Entry
	resault      *widget.Entry
	selectOption *widget.Select
}

func (a *ApiFox) LadeContent() {

	a.root = container.New(layout.NewVBoxLayout(),
		utils.RowEmpty(20),
		a.row1(),
		utils.RowEmpty(20),
		utils.Line(),
		utils.RowEmpty(20),
		core.RegisterComponent("right_content_config-api_fox-row2", a.row2()),
		utils.RowEmpty(20),
		utils.Line(),
		utils.RowEmpty(20),
		core.RegisterComponent("right_content_config-api_fox-row3", a.row3()),
		utils.RowEmpty(20),
		utils.Line(),
		utils.RowEmpty(20),
		a.row4(),
		utils.RowEmpty(30),
		a.row5(),
		utils.RowEmpty(20),
		a.row6(),
		utils.RowEmpty(30),
	)
}

func (a *ApiFox) GetContent() fyne.CanvasObject {
	return a.root
}

func (a *ApiFox) row1() fyne.CanvasObject {
	label1 := widget.NewLabel("功能:")
	//获取map的Value
	selectOption1 := widget.NewSelect([]string{
		"数据模型",
		"请求Param-编辑页",
		"请求Param-运行页",
		"请求Param-快捷请求页",
		"请求体Body-编辑页",
		"请求体Body-运行页",
		"响应Json-编辑页",
	}, nil)
	selectOption1.PlaceHolder = "请选择需要的功能"
	a.selectOption = selectOption1
	labelPadding := utils.PaddingLeftOrRight(label1, 0, 20)
	f := container.New(layout.NewFormLayout(), labelPadding, selectOption1)
	return container.NewBorder(nil, nil, utils.ColumnEmpty(100), utils.ColumnEmpty(100), f)
}

func (a *ApiFox) row2() fyne.CanvasObject {
	label1 := widget.NewLabel("添加文件:")
	input := widget.NewEntry()
	input.PlaceHolder = "请输入文件路径"

	a.files = append(a.files, input)

	f := container.New(layout.NewFormLayout(), label1, input)
	return container.NewVBox(utils.PaddingLeftOrRight(f, 100, 100))
}
func (a *ApiFox) row3() fyne.CanvasObject {
	label1 := widget.NewLabel("添加参数:")
	input := widget.NewEntry()
	input.PlaceHolder = "请输入参数"
	a.params = append(a.params, input)
	f := container.New(layout.NewFormLayout(), label1, input)
	return container.NewVBox(utils.PaddingLeftOrRight(f, 100, 100))
}
func (a *ApiFox) row4() fyne.CanvasObject {
	var addComponent = []fyne.CanvasObject{}
	addClickFile := widget.NewButtonWithIcon("加文件", theme.ContentAddIcon(), func() {
		component := core.GetComponent("right_content_config-api_fox-row2").(*fyne.Container)
		if component != nil {
			object := a.row2()
			addComponent = append(addComponent, object)
			component.Add(object)
		}
	})
	addClickParam := widget.NewButtonWithIcon("加参数", theme.ContentAddIcon(), func() {
		component := core.GetComponent("right_content_config-api_fox-row3").(*fyne.Container)
		if component != nil {
			object := a.row3()
			addComponent = append(addComponent, object)
			component.Add(object)
		}
	})

	//重置
	reset := widget.NewButtonWithIcon("重置", theme.ContentClearIcon(), func() {
		component1 := core.GetComponent("right_content_config-api_fox-row2").(*fyne.Container)
		if component1 != nil {
			for _, v := range addComponent {
				component1.Remove(v)
			}
		}
		component2 := core.GetComponent("right_content_config-api_fox-row3").(*fyne.Container)
		if component2 != nil {
			for _, v := range addComponent {
				component2.Remove(v)
			}
		}

		for _, v := range a.params {
			v.SetText("")
		}
		for _, v := range a.files {
			v.SetText("")
		}

		a.resault.SetText("")

	})

	grid := container.New(layout.NewGridLayout(3), addClickFile, addClickParam, reset)
	return container.NewBorder(nil, nil, utils.ColumnEmpty(100), utils.ColumnEmpty(100), grid)
}

func (a *ApiFox) row5() fyne.CanvasObject {

	button := widget.NewButton("生成结果", func() {
		defer func() {
			if err := recover(); err != nil {
				stack := debug.Stack()
				log.Errorf("生成结果->panic: %v\n%s", err, string(stack))
			}
		}()

		var files []string
		var params []string
		for _, v := range a.params {
			text := strings.TrimSpace(v.Text)
			//跳过空字符串
			if text == "" {
				continue
			}
			params = append(params, text)
		}

		for _, v := range a.files {
			text := strings.TrimSpace(v.Text)
			//跳过空字符串
			if text == "" {
				continue
			}
			files = append(files, text)
		}
		log.Info("==========================================")
		log.Info("files:", files)
		log.Info("params:", params)
		log.Info("selectOption:", a.selectOption.Selected)
		log.Info("==========================================")

		var result string

		switch a.selectOption.Selected {
		case "请求Param-编辑页":
			//最低1个文件
			if len(files) < 1 {
				utils.ErrorDialog("错误", "[请求Param-快捷请求页]至少需要一个接口文件", core.TopWin)
				return
			}
			interfaceFile := files[0]
			var dependFiles string
			for _, v := range files[1:] {
				dependFiles += v + ","
			}
			dependFiles = strings.TrimRight(dependFiles, ",")

			//参数只能是1个
			if len(params) != 1 {
				utils.ErrorDialog("错误", "[请求Param-快捷请求页]只需要一个参数(接口的方法名)", core.TopWin)
				return
			}
			result = apifox.JavaToApiFoxEditRequestParamCsv(interfaceFile, dependFiles, params[0])
		case "请求Param-运行页":
			//最低1个文件
			if len(files) < 1 {
				utils.ErrorDialog("错误", "[请求Param-快捷请求页]至少需要一个接口文件", core.TopWin)
				return
			}
			interfaceFile := files[0]
			var dependFiles string
			for _, v := range files[1:] {
				dependFiles += v + ","
			}
			dependFiles = strings.TrimRight(dependFiles, ",")
			//参数只能是1个
			if len(params) != 1 {
				utils.ErrorDialog("错误", "[请求Param-快捷请求页]只需要一个参数(接口的方法名)", core.TopWin)
				return
			}
			result = apifox.JavaToApiFoxRunRequestParamCsv(interfaceFile, dependFiles, params[0])

		case "请求Param-快捷请求页":

			//最低1个文件
			if len(files) < 1 {
				utils.ErrorDialog("错误", "[请求Param-快捷请求页]至少需要一个接口文件", core.TopWin)
				return
			}
			interfaceFile := files[0]
			var dependFiles string
			for _, v := range files[1:] {
				dependFiles += v + ","
			}
			dependFiles = strings.TrimRight(dependFiles, ",")
			//参数只能是1个
			if len(params) != 1 {
				utils.ErrorDialog("错误", "[请求Param-快捷请求页]只需要一个参数(接口的方法名)", core.TopWin)
				return
			}
			result = apifox.JavaToApiFoxFastRunRequestParamCsv(interfaceFile, dependFiles, params[0])
		case "请求体Body-编辑页":
			//只需要一个文件
			if len(files) != 1 {
				utils.ErrorDialog("错误", "[请求体Body-编辑页]只需要一个文件", core.TopWin)
				return
			}
			//参数为空
			if len(params) != 0 {
				utils.ErrorDialog("错误", "[请求体Body-编辑页]不需要参数", core.TopWin)
				return
			}
			result = apifox.JavaToApiFoxEditRequestBodyJson(files[0])
		case "请求体Body-运行页":
			//只需要一个文件
			if len(files) != 1 {
				utils.ErrorDialog("错误", "[请求体Body-运行页]只需要一个文件", core.TopWin)
				return
			}
			//参数为空
			if len(params) != 0 {
				utils.ErrorDialog("错误", "[请求体Body-运行页]不需要参数", core.TopWin)
				return
			}
			result = apifox.JavaToApiFoxRunRequestBodyJson(files[0])
		case "响应Json-编辑页":
			//只需要一个文件
			if len(files) != 1 {
				utils.ErrorDialog("错误", "[响应Json-编辑页]只需要一个文件", core.TopWin)
				return
			}

			//参数为空
			if len(params) == 0 {
				utils.ErrorDialog("错误", "[响应Json-编辑页]需要1个参数[page,list,detail]", core.TopWin)
				return
			}
			result = apifox.JavaToApiFoxResponseJson(files[0], params[0])

		case "数据模型":
			//只需要一个文件
			if len(files) != 1 {
				utils.ErrorDialog("错误", "[数据模型]只需要一个文件", core.TopWin)
				return
			}
			//参数为空
			if len(params) != 0 {
				utils.ErrorDialog("错误", "[数据模型]不需要参数", core.TopWin)
				return
			}
			result = apifox.JavaToApiFoxModel(files[0])
		default:
			utils.ErrorDialog("错误", "请选择功能", core.TopWin)
		}

		//获取多少行
		lines := strings.Count(result, "\n")
		//如果行数大于10行,那么就修改最小行数
		if lines > 10 {
			a.resault.SetMinRowsVisible(lines)
		}
		//4.结果展示
		a.resault.SetText(result)
	})

	return container.NewBorder(nil, nil, utils.ColumnEmpty(100), utils.ColumnEmpty(100),
		utils.Height(button, 50))
}

func (a *ApiFox) row6() fyne.CanvasObject {
	label1 := &canvas.Text{
		Text:      "结果:",
		TextStyle: fyne.TextStyle{Bold: true},
		TextSize:  16,
		Color:     common.HexToRGB("#000000"), //纯黑色
	}
	input := widget.NewMultiLineEntry()
	input.SetMinRowsVisible(10)
	a.resault = input
	return utils.PaddingLeftOrRights(100, 100, label1, input)
}
